package com.zjdiepu.www.biz.yw;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.yw.YwFeesMapper;
import com.zjdiepu.www.ibiz.yw.IYwFeesService;
import com.zjdiepu.www.model.yw.dto.YwFees;
import com.zjdiepu.www.model.yw.ext.YwFeesExt;

/**
 * 按揭日报表Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月09日 09时29分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwFeesServiceImpl extends BaseService implements IYwFeesService {
	public static final Logger logger = LoggerFactory.getLogger(YwFeesServiceImpl.class);

	@Autowired
	private YwFeesMapper ywFeesMapper;
	
	@Override
	public void saveUpdateAndDelete(YwFees ywFees, OperateType operType) {
		switch (operType) {
		case add:
			ywFees.initInsertData(ywFees.getModifier(), ywFees.getModifierName());
			ywFeesMapper.insert(ywFees);
			break;
		case edit:
			ywFees.initUpdateData(ywFees.getModifier(), ywFees.getModifierName());
			// 影响的记录数
			int updateCount = ywFeesMapper.updateByPrimaryKeySelective(ywFees);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			ywFees.initDeleteData(ywFees.getModifier(), ywFees.getModifierName());
			// 影响的记录数
			int deleteCount = ywFeesMapper.updateByPrimaryKeySelective(ywFees);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<YwFees> selectByExt(YwFeesExt ywFeesExt) {
		return ywFeesMapper.selectByExt(ywFeesExt);
	}
	
	@Override
	public int countByExt(YwFeesExt ywFeesExt){
		return ywFeesMapper.countByExt(ywFeesExt);
	}
	
	@Override
	public YwFees selectById(String id) {
		return ywFeesMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywFeesMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwFees ywFees) {
		return ywFeesMapper.countRefined(ywFees);
	}

	@Override
	public YwFees selectRefined(YwFees ywFees) {
		return ywFeesMapper.selectRefined(ywFees);
	}
	
	@Override
	public int deletedRefined(YwFees ywFees) {
		return ywFeesMapper.deletedRefined(ywFees);
	}

	@Override
	public boolean repeat(YwFees ywFees) {
		int count = countRefined(ywFees);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywFees.getId())) {
			return true;
		}
		YwFees dbItem = selectRefined(ywFees);
		if(null == dbItem) {
			return false;
		}
		return !ywFees.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywFeesMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<YwFees> ywFeess) {
		return ywFeesMapper.insertBatch(ywFeess);
	}
}
